package com.btex.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;

import com.btex.app.tools.SharedPreferencesUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.Locale;

public class LoadActivity extends Activity {
    private static final int LOAD_DISPLAY_TIME = 1500;
    private static final java.lang.String TAG = "LoadActivity";
    private SharedPreferencesUtil sp;
    private Context cxt;
    String mString = "1";
    private Resources mResources;
    private Configuration mConfiguration;
    private DisplayMetrics mDisplayMetrics;
    private boolean mIsEngilsh;
    private String mCity_new;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        StatService.start(this);
//		PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, Utils.getMetaValue(this, "api_key"));
        getWindow().setFormat(PixelFormat.RGBA_8888);
        setContentView(R.layout.activity_load);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        cxt = this;

        sp = SharedPreferencesUtil.getSharedPreferences(cxt);

        sp.putBoolean("ischengjiao", false);

        mIsEngilsh = sp.getBoolean("isEngilsh", false);
        mCity_new = sp.getString("city_new", "0");
        mResources = getResources();
        mConfiguration = mResources.getConfiguration();
        mDisplayMetrics = mResources.getDisplayMetrics();
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent i = new Intent(cxt, TabsActivity.class);
                startActivity(i);
                finish();
            }
        }, LOAD_DISPLAY_TIME);


        if (mCity_new.equals("0")) {
//            中文
            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.SIMPLIFIED_CHINESE);
            } else {
                mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);

        } else if (mCity_new.equals("1")) {
//            英文

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.US);
            } else {
                mConfiguration.locale = Locale.US;
            }

            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);

        } else if (mCity_new.equals("2")) {
//            日语
            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(Locale.JAPANESE);
            } else {
                mConfiguration.locale = Locale.JAPAN;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
        } else {

//            西班牙
            Locale spanish = new Locale("es", "rES");

            //17以上利用 setLocale（）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                mConfiguration.setLocale(spanish);
            } else {
                mConfiguration.locale = spanish;
            }
            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);


        }


//        if (!mIsEngilsh) {
//
//            mConfiguration.locale = Locale.SIMPLIFIED_CHINESE;
//            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
//
//        } else {
//
//            Locale spanish = new Locale("es", "ES");
//            mConfiguration.locale = spanish;
////            mConfiguration.locale = Locale.US;
////            mConfiguration.locale = Locale.CANADA_FRENCH;
//            mResources.updateConfiguration(mConfiguration, mDisplayMetrics);
//        }

    }

    @Override
    public void onResume() {

        if (!isTaskRoot()) {
            finish();
            return;
        }

        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPause(this);
    }


}
